using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Update.Utils.Interfaces;

namespace Update.Utils.Core
{
    /// <summary>
    /// 更新辅助类
    /// </summary>
    public class UpdateHelper : IUpdateHelper
    {
        /// <summary>
        /// 比较版本号
        /// </summary>
        /// <param name="version1">版本1</param>
        /// <param name="version2">版本2</param>
        /// <returns>0:相等, 1:version1大, -1:version2大</returns>
        public int CompareVersions(string version1, string version2)
        {
            if (string.IsNullOrEmpty(version1) && string.IsNullOrEmpty(version2))
                return 0;
                
            if (string.IsNullOrEmpty(version1))
                return -1;
                
            if (string.IsNullOrEmpty(version2))
                return 1;

            try
            {
                // 分割版本号
                string[] v1Parts = version1.Split('.');
                string[] v2Parts = version2.Split('.');
                
                // 获取最大长度
                int maxLength = Math.Max(v1Parts.Length, v2Parts.Length);
                
                // 逐位比较
                for (int i = 0; i < maxLength; i++)
                {
                    int v1Part = i < v1Parts.Length ? int.Parse(v1Parts[i]) : 0;
                    int v2Part = i < v2Parts.Length ? int.Parse(v2Parts[i]) : 0;
                    
                    if (v1Part > v2Part)
                        return 1;
                    if (v1Part < v2Part)
                        return -1;
                }
                
                return 0;
            }
            catch
            {
                // 格式错误时，尝试字符串比较
                return string.Compare(version1, version2, StringComparison.Ordinal);
            }
        }

        /// <summary>
        /// 启动目标应用程序
        /// </summary>
        /// <param name="targetAppPath">应用程序路径</param>
        /// <param name="targetAppExe">应用程序可执行文件</param>
        /// <param name="arguments">启动参数</param>
        /// <returns>是否成功启动</returns>
        public bool StartTargetApplication(string targetAppPath, string targetAppExe, string arguments = null)
        {
            try
            {
                if (string.IsNullOrEmpty(targetAppPath) || string.IsNullOrEmpty(targetAppExe))
                {
                    return false;
                }

                string exePath = Path.Combine(targetAppPath, targetAppExe);
                if (!File.Exists(exePath))
                {
                    return false;
                }

                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = exePath,
                    WorkingDirectory = targetAppPath,
                    UseShellExecute = true
                };

                if (!string.IsNullOrEmpty(arguments))
                {
                    startInfo.Arguments = arguments;
                }

                Process.Start(startInfo);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
} 